import socket
#接收
import json
sk=socket.socket()
sk.bind(('127.0.0.1',9001))
sk.listen()
conn,addr=sk.accept()
msg=conn.recv(1024).decode('utf-8')
msg=json.loads(msg)
#接收小文件
# with open(msg['filename'],'wb') as f:
#     content=conn.recv(msg['filesize'])
#     print('-->',len(content))
#     f.write(content)

#接收大文件
with open(msg['filename'],'wb') as f:
    while msg['filesize']>0:
        content=conn.recv(1024)# 文件过大时，1024个字节不会一次发过来
        #msg['filesize']-=1024 不能减1024个字节，因为不可能每次都会接收1024个字节

        msg['filesize'] -= len(content)
        f.write(content)


conn.close()
sk.close()













